package org.jeecg.modules.middleware.quartz.job.jgupload;///*
// * Copyright (c) 2020 hebeiyiliuba.com
// * All rights reserved.
// *

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.report.impl.HuaibeiReportService;
import org.jeecg.modules.wlhy.report.impl.HubeiReportService;
import org.jeecg.modules.wlhy.shipping.constant.ShippingPayStatus;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @Description: 湖北上报脚本
 * @author: duanlikao
 * @date: 2022年10月09日 11:31
 */

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class HuaibeiUploadJobService implements Job {

    public SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IHyShippingNoteService shippingNoteService;

    @Autowired
    private IHyShippingNoteBillService shippingNoteBillService;

    @Autowired
    private IHyCompanyConfigService companyConfigService;

    @Autowired
    private HuaibeiReportService huaibeiReportService;

    //@Scheduled(cron = "0 30 2 * * ?")//每天的4:30:00执行
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        if(!HyCfgUtil.isUsed2("huaibei_report")){
            log.error("淮北上报服务暂未开启,请联系管理。");
            return;
        }

        List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
                .eq("report_platform_type", ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode())
                .eq("auto_report",1)
        );
        if(CollectionUtils.isEmpty(companyConfigs)) {
            log.info("淮北监管定时上报: 没有分公司配置自动上报 !" );
            return;
        }

        Long start = System.currentTimeMillis();
        log.info("淮北监管上报运单: 执行运单上报数据开始" + now.format(new Date()));

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -60);// 把日期往后增加一天.整数往后推,负数往前移动
        Date lasDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        String startDateString =  DateUtils.date2Str(lasDate, DateUtils.date_sdf.get());
        String endDateString =  DateUtils.dateTimeToString(new Date());

        for (HyCompanyConfig companyConfig : companyConfigs) {

            QueryWrapper<HyShippingNote> queryWrapper = new QueryWrapper();
            queryWrapper.eq("shipping_pay_status","6");
            queryWrapper.eq("company_id",companyConfig.getCompanyId());
            queryWrapper.between("pay_dt",startDateString+" 00:00:00", endDateString );
            queryWrapper.orderByAsc("pay_dt");

            List<HyShippingNote> shippingNotes = shippingNoteService.list(queryWrapper);
            if(CollectionUtils.isEmpty(shippingNotes)) {
                log.info("淮北监管定时任务查找运单失败" );
                continue;
            }

            for (HyShippingNote hyShippingNote : shippingNotes){

//                QueryWrapper queryWrapper2 = new QueryWrapper();
//                queryWrapper2.eq("shipping_id", hyShippingNote.getId());
//                queryWrapper2.ne("upload_status", "1");
//                List<HyShippingNoteBill> shippingNoteBills = this.shippingNoteBillService.list(queryWrapper2);

                if (!hyShippingNote.getShippingPayStatus().equals(ShippingPayStatus.支付完成.getValue())) {
                    continue;
                }
                if (null == hyShippingNote.getUploadStatus() || !hyShippingNote.getUploadStatus().equals("1")) {
                    this.huaibeiReportService.reportShippingNote(hyShippingNote);
                    this.huaibeiReportService.reportCapitalFlow(hyShippingNote);
                }
            }
        }

        log.info("淮北监管上报运单:执行运单上报数据结束,耗时:" + (System.currentTimeMillis() - start) + "毫秒----");
        return;
    }

}
